package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.common.constant.Constant;
import com.sys.mapper.ChanageMapper;
import com.sys.mapper.DeptMapper;
import com.sys.mapper.EmployeeMapper;
import com.sys.pojo.Chanage;
import com.sys.pojo.Dept;
import com.sys.pojo.Employee;
import com.sys.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private ChanageMapper chanageMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public IPage<Employee> findListByPage(Employee employee) {
        IPage<Employee> iPage = new Page<>(employee.getPage(), employee.getLimit());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Employee::getDeleted, Constant.DELETED);
        if (!StringUtils.isEmpty(employee.getEmployeeName())) {
            queryWrapper.like(Employee::getEmployeeName, employee.getEmployeeName());
        }
        IPage<Employee> employeeIPage = employeeMapper.selectPage(iPage, queryWrapper);

        for (Employee employee1 : employeeIPage.getRecords()) {
            Dept dept = deptMapper.selectById(employee1.getDeptId());
            employee1.setDeptName(dept.getDeptName());
        }
        return employeeIPage;
    }

    @Override
    public int add(Employee employee) {
        employee.setDeleted(Constant.UNDELETED);
        return employeeMapper.insert(employee);
    }

    @Override
    public int delete(Integer id) {
        Employee employee = new Employee();
        employee.setId(id);
        employee.setDeleted(Constant.DELETED);
        return employeeMapper.updateById(employee);
    }

    @Override
    public int updateData(Employee employee) {
        if (!StringUtils.isEmpty(employee.getPositionName())) {
            Employee employee1 = employeeMapper.selectById(employee.getId());
            if (!employee.getPositionName().equals(employee1.getPositionName())) {
                Chanage chanage = new Chanage();
                chanage.setCreateBy(employee1.getUpdateBy());
                chanage.setEid(employee1.getId());
                chanage.setEname(employee1.getEmployeeName());
                chanage.setOldChanage(employee1.getPositionName());
                chanage.setNewChanage(employee.getPositionName());
                chanageMapper.insert(chanage);
            }
        }
        return employeeMapper.updateById(employee);
    }

    @Override
    public List<Employee> getAll() {
        List<Employee> employees = baseMapper.selectList(null);
        employees.forEach(employee -> {
            Dept dept = deptMapper.selectById(employee.getDeptId());
            employee.setDeptName(dept.getDeptName());
        });
        return employees;
    }
}
